package com.brt.museum.service.auth.impl;

import static com.brt.museum.vo.Consts.PAGESIZE;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.ContinueLoop;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
import org.nutz.lang.Lang;
import org.nutz.lang.LoopException;

import com.brt.museum.bean.auth.Permission;
import com.brt.museum.bean.auth.Role;
import com.brt.museum.dao.auth.IPermissionDao;
import com.brt.museum.service.auth.IPermissionService;
import com.brt.museum.vo.Consts.PermissionGroup;
import com.dgj.utils.collection.Lists;
import com.dgj.utils.db.SqlActuator;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

@IocBean
public class PermissionService implements IPermissionService {

	@Inject
	IPermissionDao permissionDao;

	@Override
	public List<Permission> findByUserId(int id) {
		final Dao dao = permissionDao.getDao();// 获取数据访问对象

		final List<Permission> permissions = Lists.newArrayList();
		// 获取配置的sql
		Sql sql = dao.sqls().create("select.direct.permission.by.user.id");
		sql.params().set("userId", id);// 注入sql参数
		List<Record> records = SqlActuator.runReport(sql, dao);// 执行sql
		Lang.each(records, new Each<Record>()// 将记录转换成对象
				{
					@Override
					public void invoke(int index, Record record, int length) throws ExitLoop, ContinueLoop, LoopException {
						permissions.add(record.toEntity(dao.getEntity(Permission.class)));
					}
				});

		return permissions;
	}

	@Override
	public List<Permission> findByRoles(List<Role> roles) {
		if (roles == null || roles.size() == 0) {
			return Lists.newArrayList();
		}
		final int[] temp = new int[roles.size()];
		Lang.each(roles, new Each<Role>() {

			@Override
			public void invoke(int index, Role ele, int length) throws ExitLoop, ContinueLoop, LoopException {
				temp[index] = ele.getId();
			}
		});

		return findByRoles(temp);
	}

	@Override
	public List<Permission> findByRoles(int[] roles) {

		if (roles == null || roles.length == 0) {
			return Lists.newArrayList();
		}

		final Dao dao = permissionDao.getDao();// 获取数据访问对象
		final List<Permission> permissions = Lists.newArrayList();
		// 获取配置的sql
		Sql sql = dao.sqls().create("find.permission.by.role");

		sql.setCondition(Cnd.where("r_id", "IN", roles));

		List<Record> records = SqlActuator.runReport(sql, dao);// 执行sql

		Lang.each(records, new Each<Record>()// 将记录转换成对象
				{

					@Override
					public void invoke(int index, Record record, int length) throws ExitLoop, ContinueLoop, LoopException {
						permissions.add(record.toEntity(dao.getEntity(Permission.class)));
					}
				});

		return permissions;
	}

	@Override
	public List<Permission> findByRoleIds(List<Integer> roles) {
		if (roles == null || roles.size() == 0) {
			return Lists.newArrayList();
		}

		final int[] temp = new int[roles.size()];

		Lang.each(roles, new Each<Integer>() {

			@Override
			public void invoke(int index, Integer ele, int length) throws ExitLoop, ContinueLoop, LoopException {
				temp[index] = ele;
			}
		});

		return findByRoles(temp);
	}

	@Override
	public Permission findByName(String name) {
		return permissionDao.findByCondition(Cnd.where("name", "=", name));
	}

	@Override
	public Permission addPermission(Permission permission) {
		return permissionDao.save(permission);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dgj.boss.service.auth.IPermissionService#findByNeedPermission(int)
	 */
	@Override
	public List<Permission> findByNeedPermission(int needPermission) {
		return permissionDao.search(Cnd.where("needPermission", "=", needPermission));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.boss.service.auth.IPermissionService#findById(int)
	 */
	@Override
	public Permission findById(int id) {
		return permissionDao.findById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dgj.boss.service.auth.IPermissionService#update(com.dgj.model.bean
	 * .auth.Permission, java.lang.String[])
	 */
	@Override
	public Result update(Permission permission, String... updateField) {
		return permissionDao.update(permission, updateField) ? Result.success() : Result.fail("更新权限信息失败");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.dgj.boss.service.auth.IPermissionService#findByUrl(java.lang.String)
	 */
	@Override
	public Permission findByUrl(String url) {
		return permissionDao.findByCondition(Cnd.where("url", "=", url));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.boss.service.auth.IPermissionService#delete(int)
	 */
	@Override
	public Result delete(int id) {
		return permissionDao.delById(id) ? Result.success() : Result.fail("删除权限失败");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.boss.service.auth.IPermissionService#findAll()
	 */
	@Override
	public List<Permission> findAll() {
		return permissionDao.findAll();
	}

	@Override
	public Pager<Record> findByPage(int page) {
		final Dao dao = permissionDao.getDao();
		Sql sql = dao.sqls().create("find.all.permissions");
		sql.params().set("pageStart", (page - 1) * PAGESIZE);
		sql.params().set("pageSize", PAGESIZE);
		Pager<Record> pager = new Pager<Record>(PAGESIZE, page);
		pager.setEntities(SqlActuator.runReport(sql, dao));
		pager.setCount(permissionDao.countAll());
		// 处理枚举
		Lang.each(pager.getEntities(), new Each<Record>() {

			@Override
			public void invoke(int index, Record record, int length) throws ExitLoop, ContinueLoop, LoopException {
				record.set("menuGroup", PermissionGroup.valueOf(record.getString("menuGroup")));
			}
		});
		return pager;
	}

	@Override
	public Pager<Record> search(int page, String key) {
		final Dao dao = permissionDao.getDao();
		Sql sql = dao.sqls().create("search.all.permissions.by.key");
		Pager<Record> pager = new Pager<Record>(PAGESIZE, page);
		sql.params().set("key", "%" + key + "%");
		sql.params().set("pageStart", (page - 1) * PAGESIZE);
		sql.params().set("pageSize", PAGESIZE);
		pager.setEntities(SqlActuator.runReport(sql, dao));
		pager.setCount(permissionDao.countByCnd(Cnd.where("name", "like", "%" + key + "%")));
		return pager;
	}

}
